﻿using Rbac.Contracts.DTO.InPut;
using Rbac.Contracts.DTO.OutPut;
using Rbac.Contracts.Interface;
using Rbac.Domain.Entity;
using Rbac.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rbac.Server
{
	/// <summary>
	/// 权限111111111
	/// 222222
	/// </summary>
	public class PermissionServer:ServerBase, IPermissionServer
	{
		private readonly IPermissionRepository _permissionRepository;
		private readonly IUserRoleRepository _userRoleRepository;
		public PermissionServer(IPermissionRepository permissionRepository, IUserRoleRepository userRoleRepository)
		{
			_permissionRepository = permissionRepository;
			_userRoleRepository = userRoleRepository;
		}
		/// <summary>
		/// 权限添加
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public int Add(PermissionInPut input)
		{
			var permiss = _mapper.Map<Permission>(input);
			permiss.CreatedTime= DateTime.Now;
			permiss.CreatedUserId = LoginUserId;
			permiss.UpdatedTime = DateTime.Now;
			permiss.IsDeleted = false;
			return _permissionRepository.Add(permiss);
		}
		/// <summary>
		/// 权限删除
		/// </summary>
		/// <param name="idList"></param>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public int Delete(params int[] idList)
		{
			var LoginUserIds = LoginUserId;
			return _permissionRepository.Delete(LoginUserIds,idList);
		}
		/// <summary>
		/// 获取权限查询
		/// </summary>
		/// <param name="pName"></param>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public List<PermissionOutPut> GetPermissionOutPuts(string pName)
		{
			var permiss=_permissionRepository.GetPermissions(pName);
			return _mapper.Map<List<PermissionOutPut>>(permiss);
		}
		/// <summary>
		/// 权限修改
		/// </summary>
		/// <param name="input">11111</param>
		/// <returns>111</returns>
		/// <param name="input"></param>
		/// <returns></returns>6656455161561515155  孟盘龙 00
		/// .
		/// <exception cref="NotImplementedException"></exception>
		public int Update(PermissionInPut input)
		{
			var permiss=_mapper.Map<Permission>(input);
			return _permissionRepository.Update(permiss);
			 
		}
		/// <summary>
		/// 判断是否有权限
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public bool Haspermission(HasPermissionInput input)
		{
			var roles = _userRoleRepository.UserRolesByUserId(LoginUserId).ToList();
			return _permissionRepository.HasPermission(roles[0].RoleId, input.ControllerName, input.ActionName);
		}
		/// <summary>
		/// 控制前段111111111122222
		/// 22222222221
		/// </summary>
		/// <param name="input">啦啦啦</param>
		/// <returns>返回</returns>
		public bool ShowButton(ShowButtonInput input)
		{
			//
			var url = input.Url.Trim('/').Split('/');
			var controller = url[0];
			var action = url[1];

			return _permissionRepository.HasPermission(input.RoleId, controller, action);

		}
	}
}
